/**
 ****************************************************************************************************
 * @file        esp_2d_disp_adapter.c
 * @author      嵌入式小书虫
 * @version     V1.0
 * @date        2020-04-17
 * @brief       my2d测试程序
 ****************************************************************************************************
 *
 * 修改说明
 * V1.0 20200417
 * 第一次发布
 <欢迎关注微信公众号：嵌入式小书虫>
 *
 ****************************************************************************************************
 */
#include "esp_2d_disp_adapter.h"
#include "esp_2d_draw.h"
extern uint16_t key_num ;
extern const esp_2d_tile_t c_tileic02GRAY8;
esp_2d_region_t s_tDirtyRegions[4] = {
    {
        .tLocation = {
            .iX = 10,
            .iY = 20,
        },
        .tSize = {
            .iWidth = 300,
            .iHeight = 10,
        },
    },
    {
        .tLocation = {
            .iX = 30,
            .iY = 50,
        },
        .tSize = {
            .iWidth = 30,
            .iHeight = 30,
        },
    },
    {
        .tLocation = {
            .iX = 1,
            .iY = 30,
        },
        .tSize = {
            .iWidth = 300,
            .iHeight = 8,
        },
    },
    {
        .tLocation = {
            .iX = 1,
            .iY = 220,
        },
        .tSize = {
            .iWidth = 300,
            .iHeight = 8,
        },
    },
    
}; 


//===============================================================
void __on_disp_adapter_load(esp_2d_op_t* pt_esp_2d_op,
                             struct esp_2d_scene *ptScene){

}
static int16_t time = 0;
int64_t my_plTimestamp = 0;
int32_t my_fps_time = 0;

 void sample_volts(void);
extern volatile uint32_t gadc_ref, gadc_bat, gadc_power ;
void __on_disp_adapter_frame_start(esp_2d_op_t* pt_esp_2d_op,
                                    struct esp_2d_scene *ptScene){
    if(esp_2d_helper_is_time_out(3000, &my_plTimestamp) ){
        sample_volts();
    }
    time++;
    if(time > 300){
        time = 0;
    }
    //my_fps_time = get_fps_time();
}
extern const esp_2d_tile_t c_tileic01RGB565;
esp_2d_fsm_rt_t __pfb_draw_my_disp_adapter_handler( 
                                          esp_2d_op_t* pt_esp_2d_op,
                                          bool bIsNewFrame){
    //my2d_op_fill_cl_t ptOP; 
                                              
    esp_2d_fill_colour(pt_esp_2d_op,NULL, ESP_2D_GLCD_COLOR_BLACK);
    esp_2d_fill_colour(pt_esp_2d_op,NULL, 0x000a);                                          
                                              
    do{
        esp_2d_region_t my_region;
        my_region.tLocation.iX = 10  + time;
        my_region.tLocation.iY = 20;
        my_region.tSize.iWidth = 10;
        my_region.tSize.iHeight = 10;
        esp_2d_fill_colour(//&ptOP,
                        pt_esp_2d_op,
                        &my_region, ESP_2D_GLCD_COLOR_RED);
        
        my_region.tLocation.iX = 30;
        my_region.tLocation.iY = 50;
        my_region.tSize.iWidth = 30;//30;
        my_region.tSize.iHeight = 30;
         esp_2dp_rgb16_tile_copy_only(  pt_esp_2d_op,
                                            &c_tileic01RGB565,
                                            //ptTile,
                                            &my_region); 
        
        esp_2d_rgb565_fill_colour_with_opacity( 
                                               pt_esp_2d_op,
                                                 &my_region,
                                               ESP_2D_GLCD_COLOR_RED,
                                                53);
        my_region.tLocation.iX = 70;
      esp_2dp_rgb16_tile_copy_keing  (  pt_esp_2d_op,
                                            &c_tileic01RGB565,
                                            //ptTile,
                                            &my_region,0); 
       esp_2d_rgb565_fill_colour_with_opacity( 
                                               pt_esp_2d_op,
                                                 &my_region,
                                               ESP_2D_GLCD_COLOR_RED,
                                                201); 
         
        my_region.tLocation.iX = 110;
        my_region.tLocation.iY = 50;
        my_region.tSize.iWidth = 100;
        my_region.tSize.iHeight = 30;     
         esp_2dp_rgb16_tile_copy_only_and_fill(pt_esp_2d_op,
                                            &c_tileic01RGB565,
                                            //ptTile,
                                            &my_region); 
        my_region.tLocation.iX = 210;
        esp_2dp_rgb16_tile_copy_keing_and_fill(pt_esp_2d_op,
                                            &c_tileic01RGB565,
                                            //ptTile,
                                            &my_region,0); 
                                                
        my_region.tLocation.iX = 10;
        my_region.tLocation.iY = 90;
        my_region.tSize.iWidth = 30;
        my_region.tSize.iHeight = 30;                                        
        esp_2dp_rgb16_tile_copy_keing_Y_mirror_image(pt_esp_2d_op,
                                                &c_tileic01RGB565,
                                            //ptTile,
                                            &my_region,0); 
        
        my_region.tLocation.iX = 50;
        esp_2dp_rgb16_tile_copy_keing_X_mirror_image(pt_esp_2d_op,
                                                    &c_tileic01RGB565,
                                            //ptTile,
                                            &my_region,0); 
        my_region.tLocation.iX = 90;                                    
        esp_2dp_rgb16_tile_copy_keing_XY_mirror_image(pt_esp_2d_op,&c_tileic01RGB565,
                                            //ptTile,
                                            &my_region,0);  
         my_region.tSize.iHeight = 15;                                    
         esp_2d_rgb565_fill_colour_with_opacity( pt_esp_2d_op,                                              
                                                 &my_region,
                                               ESP_2D_GLCD_COLOR_RED,
                                                100);                                    

        my_region.tLocation.iX = 10;
        my_region.tLocation.iY = 130;
        my_region.tSize.iWidth = 30;
        my_region.tSize.iHeight = 30; 
        esp_2dp_rgb16_tile_copy_only_and_opacity(pt_esp_2d_op,&c_tileic01RGB565,
                                            
                                            &my_region,
                                            100); 
        my_region.tLocation.iX = 50;                                    
        esp_2dp_rgb16_tile_copy_keing_and_opacity(pt_esp_2d_op,&c_tileic01RGB565,                                           
                                            &my_region,
                                            0,
                                            114); 
                                            
        my_region.tLocation.iX = 10;
        my_region.tLocation.iY = 170;
        my_region.tSize.iWidth = 30;
        my_region.tSize.iHeight = 30; 
        esp_2d_fill_colour_with_mask(pt_esp_2d_op,&c_tileic02GRAY8,                                            
                                                &my_region,
                                                ESP_2D_GLCD_COLOR_RED);  
        my_region.tLocation.iX = 50;                                           
        esp_2d_fill_colour_with_mask_and_opacity(pt_esp_2d_op,&c_tileic02GRAY8,                                            
                                                &my_region,
                                                ESP_2D_GLCD_COLOR_RED,
                                                255); 
        my_region.tLocation.iX = 90; 
        esp_2d_fill_colour_with_mask2(pt_esp_2d_op,&c_tileic02GRAY8,                                            
                                                &my_region,
                                                ESP_2D_GLCD_COLOR_RED);  

         my_region.tLocation.iX = 200;
        my_region.tLocation.iY = 200;
        for(int i = 0; i < 30; i++){
            my_region.tLocation.iX = 200 + i;
            esp_2dp_draw_point(pt_esp_2d_op,my_region.tLocation,
                                ESP_2D_GLCD_COLOR_RED); 
        }
        my_region.tLocation.iY = 210;
         for(int i = 0; i < 30; i++){
            my_region.tLocation.iX = 200 + i;
            esp_2dp_draw_point_and_opacity(pt_esp_2d_op,my_region.tLocation,
                                ESP_2D_GLCD_COLOR_RED,
                                125); 
        }
        
        
        
        esp_2d_set_lcd_text_font(esp_2d_FONT_16x24);
        esp_2d_set_lcd_text_color(ESP_2D_GLCD_COLOR_RED);  
        esp_2d_lcd_text_location(1, 1);
        esp_2d_lcd_printf("adapter");
        
        
        esp_2d_set_lcd_text_font(esp_2d_FONT_6x8);
        
        esp_2d_set_lcd_text_color(ESP_2D_GLCD_COLOR_GREEN); 
        esp_2d_lcd_text_location(30, 1);
        esp_2d_lcd_printf("k=%d,B=%d=%d=%d",key_num,gadc_ref, gadc_bat, gadc_power);
        
        
    }while(0);                                          
    return esp_2d_fsm_rt_on_going;                                            
}

esp_2d_scene_t *__esp_2d_disp_adapter_init(esp_2d_op_t* pt_esp_2d_op,
                                            esp_2d_scene_t* ptThis){
    /**ptThis = {
        .fnOnLoad =  &__on_scene1_load,
    };*/
    ptThis->fnOnLoad =  &__on_disp_adapter_load;
    ptThis->fnScene  = &__pfb_draw_my_disp_adapter_handler;
    ptThis->fnOnFrameStart = &__on_disp_adapter_frame_start;
#if ESP_2D_DIRTY_REGIONS_ENABLE       
    pt_esp_2d_op->DirtyRegions_num = 4;
    pt_esp_2d_op->ptDirtyRegions = s_tDirtyRegions;
#endif    
    return ptThis;
}

esp_2d_fsm_rt_t __pfb_draw_my_disp_adapter_navigation_handler( 
                                          esp_2d_op_t* pt_esp_2d_op,
                                          bool bIsNewFrame){
    esp_2d_set_lcd_text_font(esp_2d_FONT_6x8);
    esp_2d_set_lcd_text_color(ESP_2D_GLCD_COLOR_GREEN);                                            
    esp_2d_lcd_text_location(220, 10);
    esp_2d_lcd_printf("esp_2d-fps:%d-%dms",1000/my_fps_time,my_fps_time);                                              
    return esp_2d_fsm_rt_on_going;                                                 
}
                                          
void __on_disp_adapter_frame_navigation_start(esp_2d_op_t* pt_esp_2d_op,
                                                struct esp_2d_scene *ptScene){
    
    my_fps_time = get_fps_time();
}
esp_2d_scene_t *__esp_2d_disp_adapter_navigation_scene_init(esp_2d_op_t* pt_esp_2d_op,
                                                            esp_2d_scene_t* ptThis){
    /**ptThis = {
        .fnOnLoad =  &__on_scene1_load,
    };*/
    //ptThis->fnOnLoad =  &__on_disp_adapter_load;
    ptThis->fnScene  = &__pfb_draw_my_disp_adapter_navigation_handler;
    ptThis->fnOnFrameStart = &__on_disp_adapter_frame_navigation_start;
   
    return ptThis;
}


//=====================================================
